#!/usr/bin/env bash

## 文件路径、脚本网址、文件版本以及各种环境的判断
if [ -z "${JD_DIR}" ]
then
  ShellDir=$(cd $(dirname $0); pwd)
  ShellJd=${ShellDir}/jd.sh
else
  ShellDir=${JD_DIR}
  ShellJd=jd
fi

LogDir=${ShellDir}/log
[ ! -d ${LogDir} ] && mkdir -p ${LogDir}

ScriptsDir=${ShellDir}/scripts
ConfigDir=${ShellDir}/config
FileConf=${ConfigDir}/config.sh
FileDiy=${ConfigDir}/diy.sh
FileConfSample=${ShellDir}/sample/config.sh.sample
ListCron=${ConfigDir}/crontab.list
ListTask=${LogDir}/task.list
ListJs=${LogDir}/js.list
ListJsAdd=${LogDir}/js-add.list
ListJsDrop=${LogDir}/js-drop.list
ContentVersion=${ShellDir}/version
ContentNewTask=${ShellDir}/new_task
ContentDropTask=${ShellDir}/drop_task
SendCount=${ShellDir}/send_count

## 用户数量UserSum
function Count_UserSum {
  i=1
  while [ ${i} -le 1000 ]
  do
    TmpCK=Cookie${i}
    eval CookieTmp=$(echo \$${TmpCK})
    if [ -n "${CookieTmp}" ]
    then
      UserSum=${i}
    else
      break
    fi
    let i++
  done
}

## 把config.sh中提供的所有账户的PIN附加在jd_joy_run.js中，让各账户相互进行宠汪汪赛跑助力
function Change_JoyRunPins {
  j=${UserSum}
  PinALL=""
  while [ ${j} -ge 1 ]
  do
    TmpCK=Cookie${j}
    eval CookieTemp=$(echo \$${TmpCK})
    PinTemp=$(echo ${CookieTemp} | perl -pe "{s|.*pt_pin=(.+);|\1|; s|%|\\\x|g}")
    PinTempFormat=$(printf ${PinTemp})
    PinALL="${PinTempFormat},${PinALL}"
    let j--
  done
  PinALL="${PinALL}"
  perl -i -pe "{s|(let invite_pins = \[\")(.+\"\];?)|\1${PinALL}\2|; s|(let run_pins = \[\")(.+\"\];?)|\1${PinALL}\2|}" ${ScriptsDir}/jd_joy_run.js
}

## 将invitecode追加到脚本中
function Change_InviteCode {
  CodeZz="  'Sy7sqHks\@Sa0zZlJapLMZw9pdDQnOoo2clfysC8H5a\@S5KkcRhgdoAeEI0jznP4OcQ\@SvPp0RRoR_VHRT0c\@S5KkcRkhIoFaGdhr8lvADfA',\n  'S5KkcRkpK8QLWdU7ykvMIdw\@SaG_llbW3LM1L9qFNQWOgo2Qw\@SaXzwlYqOIvhb-KpFTXua\@Sy7sqHksZ9VM',"
  CodeJoy="'qs8m-USH6pQ=\@jl6B1p8rrYePikoy9cSB5g==\@7jPsBCIMr05nkoIt1pcU4Q==\@JvcEuk-K3Xg=\@ZkxCH5TLf-TTJi4msFDgnQ==\@3Q4uNTOdIMudlex-niqDYg=='"
  CodeGyec="'2840271\@2840729\@2841013\@2841213\@2841356\@2910623',"
  CodeJdFactory="'T0077aMrCkYCjVWnYaS5kRrbA', 'T0205KkcNWFPriOreVi95p15CjVWnYaS5kRrbA', 'T016_KgtAktLoAeEcRL8CjVWnYaS5kRrbA', 'T0086aI3H09dCjVWnYaS5kRrbA', 'T0205KkcBH1asDadQn-G6I9LCjVWnYaS5kRrbA', 'T014_KghFU1KpAePfgCjVWnYaS5kRrbA'"
  CodeSgmh="'T0077aMrCkYCjVWmIaW5kRrbA', 'T0205KkcNWFPriOreVi95p15CjVWmIaW5kRrbA', 'T016_KgtAktLoAeEcRL8CjVWmIaW5kRrbA', 'T0086aI3H09dCjVWmIaW5kRrbA', 'T0205KkcBH1asDadQn-G6I9LCjVWmIaW5kRrbA',"
  #perl -0777 -i -pe "s|(const inviteCodes = \[\n)(.+\n.+\n\])|\1${CodeZz}\n\2|" ${ScriptsDir}/jd_jdzz.js >/dev/null 2>&1
  perl -0777 -i -pe "s|(const inviteCodes = \[\n)(.+\n.+)(\n\];?)|\1${CodeJoy}\3|" ${ScriptsDir}/jd_crazy_joy.js >/dev/null 2>&1
  perl -0777 -i -pe "s|(let inviteCodes = \[\n)(.+\n.+)(\n\]?)|\1${CodeGyec}\n${CodeGyec}\3|" ${ScriptsDir}/jd_gyec.js >/dev/null 2>&1
  perl -0777 -i -pe "s|(let inviteCodes = \[\n)(.+\n.+)(\n\]?)|\1${CodeGyec}\n${CodeGyec}\3|" ${ScriptsDir}/jd_xxl.js >/dev/null 2>&1
  perl -0777 -i -pe "s|(let inviteCodes = \[\n)(.+\n.+)(\n\]?)|\1${CodeGyec}\n${CodeGyec}\3|" ${ScriptsDir}/jd_xxl_gh.js >/dev/null 2>&1
  perl -0777 -i -pe "s|(const inviteCodes = \[\n)(.+\n.+)(\n\]?)|\1${CodeJdFactory}\3|" ${ScriptsDir}/jd_jdfactory.js >/dev/null 2>&1
  perl -0777 -i -pe "s|(const inviteCodes = \[\n)(.+\n.+)(\n\]?)|\1${CodeSgmh}\n${CodeSgmh}\3|" ${ScriptsDir}/jd_sgmh.js >/dev/null 2>&1
}

## 
function Change_ALL {
  if [ -f ${FileConf} ]; then
    . ${FileConf}
    if [ -n "${Cookie1}" ]; then
      Count_UserSum
      Change_JoyRunPins
      Change_InviteCode
    fi
  fi
}

## 在日志中记录时间与路径
echo -e "\n--------------------------------------------------------------\n"
echo -n "系统时间："
echo $(date "+%Y-%m-%d %H:%M:%S")
if [ "${TZ}" = "UTC" ]; then
  echo
  echo -n "北京时间："
  echo $(date -d "8 hour" "+%Y-%m-%d %H:%M:%S")
fi
echo -e "\nSHELL脚本目录：${ShellDir}\n"
echo -e "JS脚本目录：${ScriptsDir}\n"
echo -e "--------------------------------------------------------------\n"


## clear
echo -e "clear...\n"
Change_ALL
